package adapter;

import java.util.ArrayList;

import com.activity.MainAppActivity;
import com.activity.R;

import entities.Locate;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterLocate extends ArrayAdapter<Locate> {
	private Context  context;
	private int resId;
	private ArrayList<Locate> items;

	public AdapterLocate(Context context, int resourceId,
			ArrayList<Locate> items) {
		super(context, resourceId, items);
		this.context=context;
		this.resId=resourceId;
		this.items=items;
		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView==null)
		{
			LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView=inflater.inflate(resId,null);//to get listViewItem layout
		}
		//set View in layout listviewitem from item in database
		Locate item=items.get(position);
		ImageView imgView=(ImageView) convertView.findViewById(R.id.image_locate);
		TextView distance=(TextView)convertView.findViewById(R.id.txt_distance);
		distance.setTypeface(MainAppActivity.typeFaceFont);
		
		TextView textView_name=(TextView) convertView.findViewById(R.id.txt_name);
		textView_name.setTypeface(MainAppActivity.typeFaceFont);
		
		TextView textView_address=(TextView) convertView.findViewById(R.id.txt_address);
		textView_address.setTypeface(MainAppActivity.typeFaceFont);
		
		textView_address.setText(item.getAdress());
		textView_name.setText(item.getName());
		if(item.getImages().size()>0)
			imgView.setImageBitmap(item.getImages().get(0));
		else
			imgView.setImageResource(R.drawable.nullbg);
		distance.setText(String.valueOf(item.getDistance()+" "+context.getResources().getString(R.string.unit)));
		return convertView;
	}

}
